<?php
class Exts_Gdata_Adsense
{
    

    public function getReport($login, $password, $time = 'today')
    {
        $content = file_get_contents(
            'https://www.google.com/accounts/ServiceLoginBox?service=adsense&ltmpl=login&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth'
        );

        preg_match('#<input type="hidden" name="GA3T" value="(.*?)">#', $content, $ga3t);

        $content = file_get_contents(
            'https://www.google.com/accounts/ServiceLoginBoxAuth',
            null,
            stream_context_create(
                array('http' =>
                    array(
                        'method'  => 'POST',
                        'header'  => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" . 'Cookie: GA3T=' . $ga3t[1],
                        'content' => http_build_query(
                            array(
                                'Email' => $login,
                                'Passwd' => $password,
                                'service' => 'adsense',
                                'continue' => 'https://www.google.com/adsense/login-box-gaiaauth',
                                'followup' => 'https://www.google.com/adsense/login-box-gaiaauth',
                                'ltmpl' => 'login',
                                'GA3T' => $ga3t[1]
                            )
                        )
                    )
                )
            )
        );

        $cookies = array('GAUSER=' . $login);

        foreach($http_response_header as $header) {
            if( preg_match('#^Set-Cookie: ([^;]+)#', $header, $header) > 0) {
                if( !preg_match('#EXPIRED$#', $header[1]) ) {
                    $cookies[] = $header[1];
                }
            }
        }

        preg_match('#href="(https://www\\.google\\.com/.*?)"#', $content, $url);

        $content = file_get_contents(
            $url[1],
            null,
            stream_context_create(
                array(
                    'http' =>
                    array(
                        'method'  => 'GET',
                        'header'  => 'Cookie: ' . implode('; ', $cookies)
                    )
                )
            )
        );

        $cookies = array();

        foreach($http_response_header as $header) {
            if( preg_match('#^Set-Cookie: ([^;]+)#', $header, $header) > 0) {
                if( !preg_match('#EXPIRED$#', $header[1]) ) {
                    $cookies[] = $header[1];
                }
            }
        }

        preg_match('#url=(?:\'|&\\#39;)(.*?)(?:\'|&\\#39;)"#', $content, $url);

        $content = file_get_contents( 
            str_replace( '&amp;', '&', $url[1]),
            null,
            stream_context_create(
                array(
                    'http' => array(
                        'method'  => 'GET',
                        'header'  => 'Cookie: ' . implode('; ', $cookies)
                    )
                )
            )
        );

        preg_match( 
            '#<span style="color:green;">\\$([0-9\\.]+)&nbsp;</span>#',
            $content,
            $report
        );

        return (float) $report[1];
    }

}